package fr.dsax.nounet.belote.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import fr.dsax.nounet.belote.dto.PlayerDto;
import fr.dsax.nounet.belote.jpa.Player;
import fr.dsax.nounet.belote.service.PlayerService;

@Controller
public class PlayerRestService extends GenericRestService {
	
	@Autowired
	PlayerService playerService;
	
	@RequestMapping(method=RequestMethod.POST, value="/player")
	@Transactional(readOnly = false)  
	public void createPlayer(@RequestBody PlayerDto player) {
		Player playerEntity = getDozerMapper().map(player, Player.class);
		playerService.create(playerEntity);
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/player/{playerId}")
	@ResponseBody
	public PlayerDto findGameById(@PathVariable Integer playerId) {
		Player playerEntity = playerService.findById(playerId);
		PlayerDto playerDto = getDozerMapper().map(playerEntity, PlayerDto.class);
		return playerDto;
	}
	
}
